package main

import (
	"fmt"
	"sync/atomic"
	"time"
)

func main() {
	for i := uint32(0); i < 10; i++ {
		go func(i uint32) {

			fn := func() {
				fmt.Println(i)
			}
			trigger(i, fn)
		}(i)
	}
	time.Sleep(time.Second * 5)
}

var count uint32 = 0

func trigger(i uint32, fn func()) {
	for {
		if n := atomic.LoadUint32(&count); n == i {
			fn()
			atomic.AddUint32(&count, 1)
			break
		}
		time.Sleep(time.Nanosecond)
	}
}

